package GUI;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.Histogram;
import Library.StatisticInfo;

/**
 * @author mishel hen
 *
 */
/**
 * This class display report for loans Books
 */
public class GUI_Loan_Report  extends JPanel{
	/**
	 * client console
	 */
	private ClientConsole clc;
	/**
	 * table
	 */
	private JTable table;
	/**
	 * list for statistic Info
	 */
	private StatisticInfo statisticInfo1;
	
	private Histogram h;

	/**
	 * Display statistic info to loans  
	 * @param cc
	 * @param statisticInfo
	 * @param r
	 */
	public GUI_Loan_Report (ClientConsole cc,StatisticInfo statisticInfo, final GUI_Report r) {
			
		
			setLayout(null);
			//this.userName=userName;
			this.setSize(700, 600);
			clc=cc;
			statisticInfo1=statisticInfo;
			
			JLabel lblTitle = new JLabel("Loan Report:");
			lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 20));
			lblTitle.setBounds(95, 15, 186, 25);
			add(lblTitle);
			JButton btnHistogram = new JButton(" show histogram ");
			btnHistogram.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					ArrayList<Long> list=statisticInfo1.statisticList0;
					if(list.size()>0)
					{
					Long sizeArr=(list.get(list.size()-1));
					int sizeArr1=Integer.parseInt(sizeArr.toString());
					String [] s =new String[sizeArr1+1] ;
					
					for (Integer i=0;i<sizeArr1+1;i++)
						s[i]=i.toString();
					
					int [] val =new int[sizeArr1+1];
					
					for (Integer i=0;i<list.size();i++)
					{
						int v =Integer.parseInt(list.get(i).toString());
						val[v]++;
					}
                   
                   
                    for (Integer i=0;i<sizeArr1+1;i++)
					{
                    	val[i]=val[i]*10;
					}
                    int max=val[0];
                    for (Integer i=0;i<sizeArr1+1;i++)
					{
						if(max<val[i])
							max=val[i];
					}
					h=new Histogram(s,val,max/5,100);
					int flg = h.his();
					if (flg==0)
						JOptionPane.showMessageDialog(new JFrame(),"Not enough data");
					}
					else
						JOptionPane.showMessageDialog(new JFrame(),"No values,can not be displayed Histogrma");

				}
			});
			btnHistogram.setBounds(427, 152, 166, 19);
			add(btnHistogram);
			
			
			JButton btnHistogramW = new JButton("show histogram");
			btnHistogramW.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					ArrayList<Long> list=statisticInfo1.statisticList1;
					if(list.size()>0)
					{
					Long sizeArr=(list.get(list.size()-1));
					int sizeArr1=Integer.parseInt(sizeArr.toString());
					String [] s =new String[sizeArr1+1] ;
					
					for (Integer i=0;i<sizeArr1+1;i++)
						s[i]=i.toString();
					
					int [] val =new int[sizeArr1+1];
					
					for (Integer i=0;i<list.size();i++)
					{
						int v =Integer.parseInt(list.get(i).toString());
						val[v]++;
					}
                    for (Integer i=0;i<sizeArr1+1;i++)
					{
                    	val[i]=val[i]*10;
					}
                    int max=val[0];
                    for (Integer i=0;i<sizeArr1+1;i++)
					{
						if(max<val[i])
							max=val[i];
					}


					h=new Histogram(s,val,max/5,100);
					int flg = h.his();
					if (flg==0)
						JOptionPane.showMessageDialog(new JFrame(),"Not enough data");
					}
					else
						JOptionPane.showMessageDialog(new JFrame(),"No values,can not be displayed Histogrma");

				}
			});
			btnHistogramW.setBounds(261, 152, 166, 19);
			add(btnHistogramW);

	
			JButton btnBack = new JButton("Back");
			btnBack.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					clc.cgui.setContentPane(r);
				}
			})
			;
			btnBack.setBounds(95, 370, 100, 30);
			add(btnBack);
			
			
			showResults();
			
		}
		


		public void showResults() {
			

			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(95, 100, 500, 72);
			add(scrollPane);
			
		
			
			table = new JTable(3,3);
			scrollPane.setViewportView(table);
			
			table.getColumnModel().getColumn(0).setHeaderValue("");
			table.getColumnModel().getColumn(1).setHeaderValue("Wanted");
			table.getColumnModel().getColumn(2).setHeaderValue("Normal");

							
			table.setValueAt("Average",0,0);
			table.setValueAt("Median",1,0);
			table.setValueAt("Frequency distribution",2,0);


			table.setValueAt(statisticInfo1.getAvg1(),0,1);
			table.setValueAt(statisticInfo1.getmid1(),1,1);
			table.setValueAt(statisticInfo1.getAvg0(),0,2);
			table.setValueAt(statisticInfo1.getmid0(),1,2);

					

			}
			
			
			
			
		
		}
	

		
	